package com.project.fortuneteller.controller;

import com.project.fortuneteller.context.UserContext;
import com.project.fortuneteller.service.impl.HeartServiceImpl;
import com.project.fortuneteller.utils.Result;
import com.project.fortuneteller.utils.ThreeCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 心跳的监听机制
 * @author 16905
 */
@CrossOrigin
@RestController
@RequestMapping("/heart")
public class HeartbeatController {
    // 日志系统
    private static final Logger log = LoggerFactory.getLogger(HeartbeatController.class);

    @Autowired
    private HeartServiceImpl heartServiceImpl;

    @RequestMapping("/beat")
    public Result beat(HttpServletRequest request){
        // 获取token 的值
        String tokenVal = UserContext.getUserInfo().getToken();
        // 判断 用户是否登录
        if(tokenVal == null){
            log.info("用户未登录");
        }else{
            // 去除前缀0 的方法
            int i = Long.numberOfLeadingZeros(10L);
            log.info("{} 用户在线", ThreeCache.get(tokenVal));
            return heartServiceImpl.getHeart( tokenVal);
        }

   return null;
    }
}
